package com.example.idachuappone.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;

import com.example.idachuappone.R;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class RefreshListView extends ListView implements OnScrollListener {
	private Button btn_add;
	private View footerView;
	private int footerViewHeight;
	private ImageView img_none_data;
	private boolean isLoadMoring;
	private boolean isScrollToBottom;
	private boolean isadddata;
	private LinearLayout ll_add_data;
	private OnRefreshListener mOnRefreshListener;

	public RefreshListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.isLoadMoring = false;
		this.isadddata = true;
		initFooter();
		setOnScrollListener(this);
	}

	private void initFooter() {
		this.footerView = LayoutInflater.from(getContext()).inflate(R.layout.listview_footer, null);
		this.btn_add = (Button) this.footerView.findViewById(R.id.btn_add);
		this.btn_add.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!RefreshListView.this.isLoadMoring) {
					RefreshListView.this.ll_add_data.setVisibility(0);
					RefreshListView.this.btn_add.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
					RefreshListView.this.img_none_data
							.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
					RefreshListView.this.mOnRefreshListener.onLoadMoring();
				}
			}
		});
		this.img_none_data = (ImageView) this.footerView.findViewById(R.id.img_none_data);
		this.ll_add_data = (LinearLayout) this.footerView.findViewById(R.id.ll_add_data);
		this.footerView.measure(0, 0);
		this.footerViewHeight = this.footerView.getMeasuredHeight();
		this.footerView.setPadding(0, -this.footerViewHeight, 0, 0);
		addFooterView(this.footerView);
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		if ((scrollState == 0 || scrollState == 2) && this.isScrollToBottom && !this.isLoadMoring) {
			this.isLoadMoring = true;
			this.footerView.setPadding(0, 0, 0, 0);
			setSelection(getCount());
			if (this.mOnRefreshListener != null && this.isadddata) {
				this.ll_add_data.setVisibility(0);
				this.btn_add.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				this.img_none_data.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
				this.mOnRefreshListener.onLoadMoring();
			}
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		if (firstVisibleItem + visibleItemCount == totalItemCount) {
			this.isScrollToBottom = true;
		} else {
			this.isScrollToBottom = false;
		}
	}

	public void setOnRefreshListener(OnRefreshListener listener) {
		this.mOnRefreshListener = listener;
	}

	public void hideFooter() {
		this.footerView.setPadding(0, -this.footerViewHeight, 0, 0);
	}

	public void hideFooterView(boolean isadd) {
		this.isadddata = isadd;
		if (isadd) {
			this.ll_add_data.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.btn_add.setVisibility(0);
			this.img_none_data.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
		} else {
			this.ll_add_data.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.btn_add.setVisibility(BitmapCacheManagementTask.MESSAGE_CLEAR_MEMORY_BY_KEY);
			this.img_none_data.setVisibility(0);
		}
		this.isLoadMoring = false;
	}
}
